import * as THREE from 'three';
import { OrbitControls } from 'three/examples/jsm/controls/OrbitControls';

// 创建场景
const scene = new THREE.Scene();

// 创建相机
const camera = new THREE.PerspectiveCamera(75, window.innerWidth / window.innerHeight, 0.1, 1000);
camera.position.set(600, 600, 600);

// 创建渲染器
const renderer = new THREE.WebGLRenderer();
renderer.setSize(window.innerWidth, window.innerHeight);
document.body.appendChild(renderer.domElement);

// 创建轨道控制器
const controls = new OrbitControls(camera, renderer.domElement);
controls.enableDamping = true; // 启用阻尼，使相机运动更平滑
controls.addEventListener('change', () => {
    renderer.render(scene, camera); // 有动画渲染这里就可以不用渲染
});

const init = (el: Element) => {
    if(el) {
        const rect = el.getBoundingClientRect();
        const width = rect.width;
        const height = rect.height;
        renderer.setSize(width, height);
        camera.aspect = width / height;
        camera.updateProjectionMatrix();
        renderer.render(scene, camera);
        el.appendChild(renderer.domElement);
    } else {
        renderer.render(scene, camera);
        document.body.appendChild(renderer.domElement);
    }
}

const threeObj = {
    scene,
    camera,
    renderer,
    controls,
    init
}

export { threeObj, THREE, OrbitControls };